<script lang="ts">
import { defineComponent } from "vue";
import $config from "./common/config";
import $bridge from "@/static/js/dsbridge.js";
import { connectWebsocket, setConfig, beforeRouterInvoke } from "./common/libs/dependency";
import { getClient, parseJSON, uniNavigator } from "./common/libs/utils";

export default defineComponent<{ globalData: typeof $config }>({
  globalData: { $config, $bridge },
  onLaunch(e) {
    // #ifndef APP-PLUS
    console.log(
      `%c ${$config.APP_NAME} %c Version ${$config.APP_VERSION} %c`,
      "background: #35495e; padding: 1px; border-radius: 3px 0 0 3px; color: #fff",
      "background: #007aff; padding: 1px; margin: 1px; border-radius: 0 3px 3px 0; color: #fff; font-weight: bold;",
      "background: transparent"
    );
    // #endif

    uni.onTabBarMidButtonTap(() => {
      uni.showToast({ title: "点击了中间按钮~", icon: "none" });
    });

    e?.path && beforeRouterInvoke(e.path);
    // uni.setLocale("zh");
    connectWebsocket();
    setConfig();

    $bridge.call?.("fnTool.setStatusBarColor", { color: $config.COLOR_10, isBlack: false });
  },
  async onShow() {
    const matchText = (data: string) => {
      const result = data.match(new RegExp(`^【${$config.APP_NAME}】「(.*.)」(.*.)-(.*.) 复制内容打开APP查看$`));
      if (result) {
        const [, content, type, sn] = result;
        uni.showModal({
          title: "朋友分享了一件商品",
          content,
          confirmText: "去查看",
          success({ cancel }) {
            uni.setClipboardData({ data: " ", showToast: false });
            if (cancel) return;
            +type === 1 && uniNavigator({ url: `/pages/client/mall/goodsInfo?sn=${sn}` });
          }
        });
      }
    }

    switch (getClient()) {
      case 9:
      case 10:
        try {
          await uni.getStorage({ key: "agreedAgreement" });
        } catch (error) {
          return error;
        }
        $bridge.call?.("fnTool.getClipContent", {}, (result: string) => {
          const res = parseJSON(result) || {};
          if (+res.code !== 200) return;
          matchText(res.content);
        });
        break;
      default:
        // #ifdef H5
        if (!navigator.clipboard) return;
        // #endif
        uni.getClipboardData({
          success({ data }: any) {
            matchText(data);
          }
        });
        break;
    }
  },
  onPageNotFound({ path }: App.PageNotFoundOption) {
    path !== "/" && uni.redirectTo({ url: $config.PAGE_NOT_FOUND });
  },
  onThemeChange({ theme }: { theme: "light" | "dark" }) {
    plus.nativeUI.setUIStyle(theme);
    this.$store.commit("setState", { key: "theme", value: theme });
  },
});
</script>

<style lang="scss">
@import "@/common/style/reset.scss";
@import "@/common/style/theme.scss";
</style>
